
import 'package:ac/data/ac_device_tool.dart';
import 'package:ac/data/ac_global_controller.dart';
import 'package:dio/dio.dart';
import 'package:get/get.dart';

class AcTranslationUtil{
  static Future<String> translation(String text) async {
    var urlParams = {
      "key": Get.find<AcGlobalController>().googleKey.value,
      "target": AcDeviceTool().foLocalLanguageCode, // 目标语言Code，从设备中获取当前语言。
      "q": text, // 被翻译的文案
      "format": 'text'
    };
    Dio dio = Dio();
    dio.options.headers = {
      'Content-Type': 'application/json',
    };
    var response = await dio.post(
      "https://translation.googleapis.com/language/translate/v2",
      queryParameters: urlParams,
    );
    if (response.statusCode == 200) {
      var result = response.data;
      return result['data']['translations'][0]['translatedText'];
    }
    return "";
  }
}